﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace Console
{
    public partial class Form0 : Form
    {
        private string account_path;
        private string admin_path;
        private bool admin = false;
        
        public Form0()
        {
            InitializeComponent();


            this.KeyPreview = true;
            this.KeyDown += Form0_KeyDown;
            System.Console.WriteLine(Environment.CurrentDirectory);
            account_path = Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "accounts.txt";
            if (!File.Exists(account_path))
            {
                File.Create(account_path).Close();
            }
            admin_path = Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "admin.txt";
            if (!File.Exists(admin_path))
            {
                File.Create(admin_path).Close();
                File.WriteAllText(admin_path, HashPassword("123456"));
            }
        }

        private void Form0_Load(object sender, EventArgs e)
        {
            // Label3居中
            label3.Left = (ClientSize.Width - label3.Width) / 2;

            // 计算Label和TextBox的中心高度差
            int delta1 = (label1.Height - textBox1.Height) / 2;
            int delta2 = (label2.Height - textBox2.Height) / 2;

            // 设置TextBox的Top属性，使其与Label的中心对齐
            textBox1.Top = label1.Top + delta1;
            textBox2.Top = label2.Top + delta2;

            // 设置textBox的Left属性，使其在窗体中居中
            textBox1.Left = (ClientSize.Width - textBox1.Width) / 2;
            label1.Left = textBox1.Left - label1.Width;
            textBox2.Left = (ClientSize.Width - textBox2.Width) / 2; ;
            label2.Left = textBox2.Left - label2.Width;

            // 登录按钮居中
            button1.Left = (ClientSize.Width - button1.Width) / 2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (!File.Exists(admin_path) || !File.Exists(account_path))
            {
                MessageBox.Show("请重启程序");
                Application.Exit();
            }

            string username = textBox1.Text;
            string password = textBox2.Text;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                MessageBox.Show("用户名和密码不能为空");
                return;
            }

            if (username == "root")
            {
                string password0 = File.ReadAllText(admin_path);
                if (HashPassword(password) == password0)
                {
                    this.admin = true;
                    MessageBox.Show("管理员登录成功");
                    Login();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                }
                return;
            }
            else
            {
                string[] lines = File.ReadAllLines(account_path);
                foreach (string line in lines)
                {
                    string[] account = line.Split(',');
                    if (account[0] == username)
                    {
                        string hashedPassword = HashPassword(password);
                        if (account[1] == hashedPassword)
                        {
                            this.admin = false;
                            MessageBox.Show("游客登录成功");
                            Login();
                            return;
                        }

                    }
                }
                MessageBox.Show("账号或密码错误");
                return;
            }


            //if (textBox1.Text)
            //    string[] lines = File.ReadAllLines(FilePath);
            //foreach (string line in lines)
            //{
            //    string[] parts = line.Split(',');
            //    if (parts[0] == username)
            //    {
            //        MessageBox.Show("用户名已存在");
            //        return;
            //    }
            //}


        }
        
        private string HashPassword(string password)
        {
            using (SHA256 sha256Hash = SHA256.Create())
            {
                byte[] data = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(password));
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sb.Append(data[i].ToString("x2"));
                }
                return sb.ToString();
            }
        }

        private void Login()
        {
            Form1 form1 = new Form1(this.admin);
            this.Hide();
            form1.ShowDialog();
            textBox1.Text = "";
            textBox2.Text = "";
            this.Show();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text == "root")
            {
                this.admin = true;
            }
        }

        private void Form0_KeyDown(object sender, KeyEventArgs e)
        {
            // 判断按下的键是否为 回车 键
            if (e.KeyCode == Keys.Enter)
            {
                button1.PerformClick();
            }
        }
    }
}
